package com.yitiao.data_algorithms.leetcode.editor.cn;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

class MinimumDepthOfBinaryTree{
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    // 层次遍历
    public int minDepth(TreeNode root) {
        if (root == null)return 0;

        int depth = 1;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            int n = queue.size();
            for (int i = 0; i < n; i++) {
                TreeNode node = queue.poll();
                if (node.left==null&&node.right==null){
                    return depth;
                }
                if (node.left!=null){
                    queue.offer(node.left);

                }
                if (node.right!=null){
                    queue.offer(node.right);
                }
            }
            depth++;
        }
        return depth;
    }

    // 递归
    private int extracted(TreeNode root) {
        if (root ==null){
            return 0;
        }
        int left = minDepth(root.left);
        int right = minDepth(root.right);
        if (root.left==null|| root.right==null){
            return left+right+1;
        }
        return Math.min(left,right)+1;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}